var TableDatatablesAjax = function () {
    var datatableAjax = function () {
        dt = $('.dataTablesAjax');
        ajax_datatable = dt.DataTable({
            "processing": true,
            "serverSide": true,
            "searching": true,
            "searchDelay": 800,
            "search": {
                "regex": true
            },
            "ajax": {
                'url': '/admin/eticket/activity/ajaxIndex'
            },
            "pagingType": "full_numbers",
            "orderCellsTop": true,
            "dom": '<"html5buttons"B>lTfgitp',
            "buttons": [
                {extend: 'copy', title: 'permission'},
                {extend: 'csv', title: 'permission'},
                {extend: 'excel', title: 'permission'},
                {extend: 'pdf', title: 'permission'},
                {
                    extend: 'print',
                    customize: function (win) {
                        $(win.document.body).addClass('white-bg');
                        $(win.document.body).css('font-size', '10px');
                        $(win.document.body).find('table')
                            .addClass('compact')
                            .css('font-size', 'inherit');
                    }
                }
            ],
            "columns": [
                {
                    "data": "id",
                    "name": "id"
                },
                {
                    "data": "name",
                    "name": "name",
                    "orderable": true
                },
                {
                    "data": "school",
                    "name": "school",
                    "orderable": true
                },
                {
                    "data": "location",
                    "name": "location",
                    "orderable": true
                },
                {
                    "data": "start_date",
                    "name": "start_date",
                    "orderable": true
                },
                {
                    "data": "end_date",
                    "name": "end_date",
                    "orderable": true
                },
                {
                    "data": "status",
                    "name": "status",
                    "orderable": true
                },
                {
                    "data": "actionButton",
                    "name": "actionButton",
                    "type": "html",
                    "orderable": false
                }
            ],
            "drawCallback": function (settings) {
                ajax_datatable.$('.tooltips').tooltip({
                    placement: 'top',
                    html: true
                });

                var $dateTh = ajax_datatable.columns('.date-th').nodes().flatten().to$();
                $dateTh.each(function (i, item) {
                    console.log($(item).text());
                    var date = new Date(parseInt($(item).text())*1000);
                    var Y = date.getFullYear() + '-';
                    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
                    var D = date.getDate() + ' ';
                    var h = date.getHours() + ':';
                    var m = date.getMinutes();
                    $(item).text(Y + M + D + h + m);
                });
                var $statusLi = ajax_datatable.columns('.status').nodes().flatten().to$();
                $statusLi.each(function (i, item) {
                    if ($(item).text() == '0') {
                        $(item).html('开启')
                    } else if ($(item).text() == '1') {
                        $(item).html('<span style="color: red">关闭</span>')
                    }
                });
            },
            "language": {
                url: '/admin/i18n'
            }
        });
    };
    return {
        init: datatableAjax
    }
}();
$(function () {
    TableDatatablesAjax.init();
});
